package com.kumi.crowd.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kumi.crowd.bean.Admin;
import com.kumi.crowd.bean.AdminExample;
import com.kumi.crowd.constant.CrowdConstant;
import com.kumi.crowd.exception.LoginAcctAlreadyInUseException;
import com.kumi.crowd.exception.LoginFailedException;
import com.kumi.crowd.mapper.AdminMapper;
import com.kumi.crowd.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;
    @Override
    public void save(Admin admin) {
        adminMapper.insert(admin);
    }

    @Override
    public List<Admin> getAll() {
        return adminMapper.selectByExample(new AdminExample());
    }

    @Override
    public Admin getAdminByLoginAcct(String loginAcc, String userPswd) {
        AdminExample adminExample = new AdminExample();
        AdminExample.Criteria criteria = adminExample.createCriteria();
        criteria.andLoginAcctEqualTo(loginAcc);
        List<Admin> list = adminMapper.selectByExample(adminExample);
        if (list == null || list.size() == 0) throw new  LoginFailedException(CrowdConstant.MESSAGE_LOGIN_FAILED);


        if (list.size() > 1) throw new RuntimeException(CrowdConstant.MESSAGE_SYSTEM_ERROR_LOGIN_NOT_UNIQUE);

        Admin admin = list.get(0);
        if (admin == null) throw new LoginFailedException(CrowdConstant.MESSAGE_LOGIN_FAILED);

        String userPswdData = admin.getUserPswd();

        String password = SecureUtil.md5(userPswd);
        if (!Objects.equals(userPswdData,password)) throw new LoginFailedException(CrowdConstant.MESSAGE_LOGIN_FAILED);

        return admin;
    }

    @Override
    public PageInfo<Admin> getPage(String keyword, Integer pageNum, Integer pageSize) {

        PageHelper.startPage(pageNum,pageSize);

        List<Admin> list = adminMapper.selectAdminByKeyword(keyword);

        return new PageInfo<>(list);
    }

    @Override
    public void remove(Integer id) {
        adminMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void add(Admin admin) {
        String password = SecureUtil.md5(admin.getUserPswd());

        admin.setUserPswd(password);

        Date date = new Date();

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String format1 = format.format(date);

        admin.setCreateTime(format1);

        try {
            adminMapper.insert(admin);
        } catch (Exception e) {

            if(e instanceof DuplicateKeyException) {
                throw new LoginAcctAlreadyInUseException(CrowdConstant.MESSAGE_LOGIN_ACCT_ALREADY_IN_USE);
            }
        }
    }

    @Override
    public Admin getById(Integer id) {
        return adminMapper.selectByPrimaryKey(id);
    }

    @Override
    public void update(Admin admin) {
        try {
            adminMapper.updateByPrimaryKeySelective(admin);
        } catch (Exception e) {
            e.printStackTrace();
            if(e instanceof DuplicateKeyException) {
                throw new LoginAcctAlreadyInUseException(CrowdConstant.MESSAGE_LOGIN_ACCT_ALREADY_IN_USE);
            }
        }
    }

    @Override
    public void addRoleAndAdmin(Integer id, List<Integer> roleId) {
        adminMapper.deleteRelationShip(id);

        if (roleId != null && roleId.size() > 0){
            adminMapper.insertRelationShip(id,roleId);
        }
    }
}
